Cogito, ergo sum

Legacy:Dynamic Array/UnrealEngine 1

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

This is an example of Scripting Operators.

Always wanted to use those elusive array<>'s, but found the GetPropertyText/SetPropertyText interface to be a problem? This is where operators come in. Here I provide a simple interface to a dynamic array of integers (although this is easily adaptable to support various types). You set elements of a dynamic array with 'Array << (Index:Data)' (totally arbitrary syntax I made up), and access an element with 'Array<Index>'.

class TestDynArray extends CommandLet;
// Our struct to store the index/data pairs created using the : operator
struct SetGroup
{
  var int Index;
  var int NData;
};
 
// The left side of the Array construct... the one that actually does something.
final operator(50) int < ( DynArray A, int B )
{
  return A.Get( B );
}
 
// The right side of the Array construct... does absolutely nothing, it's just there to make the syntax pretty (complete the brackets).
final postoperator int > ( int A )
{
  return A;
}
 
// Sets an element in a dynamic array, taking a index/data pair as the right side.
final operator(50) DynArray << ( out DynArray A, SetGroup B )
{
  A.Set( B.Index, B.NData );
  return A;
}
 
// Creates a index/data pair
final operator(23) SetGroup : ( int A, int B )
{
  local SetGroup C;
  C.Index = A;
  C.NData = B;
  return C;
}
 
// Just a test function to show that we can use all sorts of expressions within the index/data pairs
function int TestFunc()
{
  return 10;
}
 
function int main( string parm )
{
  local DynArray Arr;
  local int i;
  local setgroup g;
 
  // Instantiate a DynArray
  Arr = new class'DynArray';
 
  // Set some elements
  Arr << (5:78);
  Arr << (1:30);
  Arr << (TestFunc():69);
 
  // And log them
  Log( Arr<5> @ Arr<1> @ Arr<TestFunc()> );
  return 1;
}
// Interface to dynamic arrays. Evil.
class DynArray extends Object;
 
var array<int> Data;
var int CacheData[1024];
var int Num;
var bool bCached;
 
// Parse the elements out of a string like (2,3,5,2,4,6,2), and store them in our cache
function Recache()
{
  local int i;
  local string Nightmare;
  local int NextPos;
  local int z;
  Num = 0;
  Nightmare = GetPropertyText("Data");
  Nightmare = Right( NightMare, Len(NightMare)-1 );
  Nightmare = Left( NightMare, Len(NightMare)-1 );
  for(i = InStr( Nightmare, "," );i > 0;z++)
  {
    CacheData[Num++] = int( Left(Nightmare, i) );
    Nightmare = Mid( Nightmare, i + 1, Len( Nightmare ) );
    i = InStr( Nightmare, "," );
    if ( i == -1 && Len(NightMare) > 0 )
      CacheData[Num++] = int( Nightmare );
  }
  bCached = true;
}
 
// Set an element by building a string like (3,3,5,9), and recache.
function Set( int Index, int Data )
{
  local string Build;
  local int i;
  Recache();
  CacheData[Index] = Data;
  if ( Index > Num-1 )
    Num = Index+1;
  Build = Build $ "(";
  for(i=0;i<Num;i++)
  {
    Build = Build $ CacheData[i];
    if ( i != Num-1 )
      Build = Build $ ",";
  }
  Build = Build $ ")";
  SetPropertyText("Data", Build);
  bCached = true;
}
 
// Get a cached element
function int Get( int Index )
{
  if ( !bCached )
    Recache();
 
  return CacheData[Index];
}